view and Manipulate-Files
Viewing files and Text Manipulation
Introduction:
- In Linux nearly everything u deal with directly is a file and most importantly configuration files.
- All Configuration files are treated as Text file we open them and
Manupilate/changethem as we need.
for illustration i will use snort config file to view and manipulate
sudo apt install snort
Viewing files:
-
catis most common and basic way to view a file, however it has limitations. -
this will display all config file of snort.
head and tail commands:
- rather than using
catwe can use these options(head,tail)to display a part of the content in a file, by default they display 10 lines content of a file. - Dont forget to visit there manual
tail --helporman headto find more options. - u can to many complex funtion unlike the using
cat. head: Beginning content of a file.tailEnding content of a file.
head: -(number of lines switch)
- if u wanna view the specific amount content we use -(n) switch
- first
20lines example.head -20 /etc/snort/snort.lua
tail: -(number of lines switch)
- if u wanna view the specific amount content we use -(n) switch
- last
20lines example.tail -20 /etc/snort/snort.lua
nl Numbering the Lines:
- We can view the number of the lines by using
nlcommand. - In this example this config file has
220lines. - Keep that in mind that we use
nlit skips all blank lines.
note: there is famous github in zsh shell u can add that can show number of lines and syntax highlighting of the code called batcat its just better version of cat but not do research about it before using it.
Viewing Files with less and more
In Linux, you can use less and more to view the contents of a file in the terminal — especially useful for reading long files.
less
- Think of
lesslike reading a man page. - It shows one screen at a time, but you can scroll up and down using:
- Arrow keys
- Page Up / Page Down
- Search with
/
- It's more powerful and flexible than
more.
Example:
less filename.txt
Viewing Files with less and more
- In Linux, you can use
lessandmoreto view the contents of a file in the terminal especially useful for reading long files.
Tip: Use lesswhenever possible it’s more modern and user-friendly.
less
- Think of
lesslike reading a man page. - It shows one screen at a time, but you can scroll up and down using:
- Arrow keys
- Page Up / Page Down
- Search with
/
- It's more powerful and flexible than
more.
Example:
less filename.txt
or
nl /etc/snort/snort.lua | less
more
moredisplays the file page by page.- Press:
Spaceto go to the next pageEnterto go line by line
- You can’t scroll back up, only forward.
Example:
more filename.txt
or
nl /etc/snort/snort.lua | more
File Manipulation
Utilizing grep for Text Manipulation and Viewing files:
- for example we can achieve this by piping the output of
nlview the file where occurance of a certain word, - ex:
nl /etc/snort/snort.lua | grep configure
lets see how complex when we can combine the head or tail, nl and cat with grep .
-
in this example i will use
nlto output the file with number of lines and usegrepto capture and shorten the output to display the first 4 lines the when statement in lua programming language this is what snort is currently is built with. -
without combining it with
head or tail.
-
combining it with
headortail:
cat /etc/snort/snort.lua | grep when | head -4 | cat > test.txt | nl test.txt
- in this example we combined the commands using Piping( | ) to run these commands. I searched and viewed for
whenstatement from snort.lua, take the first 4, save them to test.txt, then try to number them using nl.
Remark:| (Pipe): Passes output of one command as input to another command.
sed Command: The stream editor
-
view manual about sed first i will cover only the basics of
man sed or sed --help. -
sed command can search for occurence of a word inside the File
and preform an Action. -
it can find and replace content of a file
-
lets work on this file
test
cat /opt/metasploit-framework/embedded/framework/data/wordlists/password.lst | head -10 | cat > test && cat test
- if u dont have the wordlist file just copy this in a file.
!@#$%
!@#$%^
!@#$%^&
!@#$%^&*
!boerbul
!boerseun
!gatvol
!hotnot
!kak
!koedoe
Search in the file using sed:
sed -n /text-to-search/p-n: Suppresses automatic printing —sedwon’t show every line by default./text-to-search/: The pattern you want to search for.p: Prints only the lines that match the pattern.
With -n, sed shows only matching lines.
- see the examples:
Other Example of searching with sed using Pipe( | ):
Manipulate a file content:
Syntax: echo "sed <flag1>/textiwanttochange/New-text-/<flag2> [filepath full path > newpath]"
flags to use to manipulate a file sand g
s == subtitute the text to new text
g == global - all occurance of the word repeated multiple types
- added a multiple occurance to test the
gflag of the word!hotnot
- Run the command
sed s/hotnot/I-manipulated-you/g ~/test > test2
Challenge
Before you move on Try to solve this challenge, try out the skill.you learned:
- Navigate to /usr/share/metasploit-framework/data/wordlists. This is a,directory of multiple wordlists that can be used to brute force passwords.
in various password-protected devices using Metasploit, the most popular pentesting and hacking framework. - Use the cat command to view the contents of the file password.lst.,
- Use the more command to display the file password.lst.,
- Use the less command to view the file password.lst.,
- Now use the nl command to place line numbers on the passwords in,password.lst. There should be around 88,396 passwords.
- Use the tail command to see the last 20 passwords in password.lst.,
- Use the cat command to display password.Ist and pipe it to find all the passwords that contain 123.
finding wordlist since the directory of the wordlist changed.
sudo find / -type f -name "*word.lst"
cat or nl
nl /opt/metasploit-framework/embedded/framework/data/wordlists/password.lst
- finding the last 20 passwords:
added more line of command save it in a file and show the output
nl /opt/metasploit-framework/embedded/framework/data/wordlists/password.lst | tail -20 | cat > test && nl test
finding the passwords with 123 in them
-
Note:When you're searching for something like numbers using grep, avoid using
nl(which adds line numbers), because it can interfere with your search results. The line numbers added bynlmight confuse grep and give you inaccurate matches. look in the above Screenshot.
-
cat /opt/metasploit-framework/embedded/framework/data/wordlists/password.lst | grep 123 -
Instead, use cat to display the file contents and pipe it directly to grep, like this:
